using System;
using System.Collections.Generic;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMUniqueConstraintMustHaveProps
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMUniqueConstraintMustHaveProps

        [Test]
        public void Test_CreateDMUniqueConstraintMustHavePropsRule()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            List<DMProperty> properties = new List<DMProperty> {property};
            DMUniqueConstraint uniqueConstraint = dmClass.CreateUnsavedUniqueConstraint(properties);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uniqueConstraint);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMUniqueConstraintMustHaveProps();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMUniqueConstraintMustHaveProps", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
        }

//        [Test]
//        public void Test_CreateDMUniqueConstraintMustHavePropsRule_WhenClassNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMUniqueConstraintMustHaveProps(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//                //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmUniqueConstraint", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_DMUniqueConstraintMustHaveProps_FailsIfNoObjectID()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            List<DMProperty> properties = new List<DMProperty> { property };
            DMUniqueConstraint uniqueConstraint = dmClass.CreateUnsavedUniqueConstraint(properties);
            IBusinessObjectRule rule = new DMUniqueConstraintMustHaveProps();
            uniqueConstraint.UniqueConstraintProperties.Clear();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, uniqueConstraint.UniqueConstraintProperties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(uniqueConstraint);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The UniqueConstraint '", rule.Message);
            StringAssert.Contains("'" + uniqueConstraint.ToString() + "' on Class '" + uniqueConstraint.Class + "'", rule.Message);
            StringAssert.Contains("' must have at least one Property", rule.Message);
        }

        [Test]
        public void Test_DMUniqueConstraintMustHaveProps_FailsIfHasObjectIDWithNoProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMProperty property = dmClass.Properties.CreateBusinessObject();
            List<DMProperty> properties = new List<DMProperty> { property };
            DMUniqueConstraint uniqueConstraint = dmClass.CreateUnsavedUniqueConstraint(properties);
            IBusinessObjectRule rule = new DMUniqueConstraintMustHaveProps();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, uniqueConstraint.UniqueConstraintProperties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(uniqueConstraint);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        #endregion
    }



}